package cc.lzmsh.controller;

import cc.lzmsh.pojo.Category;
import cc.lzmsh.pojo.result.Result;
import cc.lzmsh.service.CategoryService;
import cc.lzmsh.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * Author:   msh
 * Date:     2024/9/26 上午9:30
 * Description:
 */

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody Category category) {
        Map<String, Integer> map = ThreadLocalUtil.get();
        category.setCreateUser(map.get("userId"));
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        return categoryService.addCategory(category);
    }

    /**
     * 获取当前已登录用户创建的所有文章分类
     */
    @GetMapping
    public Result list() {
        Map<String, Integer> map = ThreadLocalUtil.get();
        return categoryService.list(map.get("userId"));
    }

    /**
     * 根据ID获取文章分类详情
     */
    @GetMapping("/detail/{id}")
    public Result getCategoryById(@PathVariable Integer id) {
        return categoryService.getCategoryById(id);
    }

    /**
     * 更新文章分类
     */
    @PutMapping
    public Result updateCategory(@RequestBody Category category) {
        return categoryService.updateCategory(category);
    }

    /**
     * 根据ID删除文章分类
     */
    @DeleteMapping("/{id}")
    public Result deleteCategory(@PathVariable Integer id) {
        return categoryService.deleteCategory(id);
    }
}
